﻿using System;

using System.Windows.Forms;
using FoundationClass;
using FoundationClass.Entity;
using Telerik.WinControls.UI;

namespace P2MARY_INTERNET.Controls
{
    public partial class PromotionForm : UserControl
    {
        public PromotionForm()
        {
            InitializeComponent();
            InitializePromotionData();
            InitializeControl();
            lblValidate.Text = string.Empty;
        }
        private void InitializeControl()
        {
            pnlPromotionData.Enabled = btnDeletePro.Enabled = lstPromotion.SelectedIndex > -1;
        }

        private void InitializePromotionData()
        {
            try
            {
                var promotionList = MasterDataHandler.GetAllPromotions();
                if (promotionList == null)
                    return;

                foreach (var t in promotionList)
                {
                    var item = new RadListDataItem(t.Name, t);
                    lstPromotion.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(@"InitializePromotionData : "+ ex.Message +@" | "+ ex.StackTrace);
            }
        }

        private void lstPromotion_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            try
            {
                if (lstPromotion.SelectedIndex == -1)
                {
                    ClearForm();
                    return;
                }
                pnlPromotionData.Enabled = btnDeletePro.Enabled = lstPromotion.SelectedIndex > -1;

                var promotion = lstPromotion.SelectedItem.Value as Promotion;
                if (promotion == null) return;

                txtProName.Text = promotion.Name;
                radStartDate.Value = promotion.StartDate;
                radEndDate.Value = promotion.EndDate;
                if (promotion.StartTime == DateTime.Parse("01/01/1753 12:00:00 AM"))
                {
                    chkTime.IsChecked = false;
                    drpStartTime.SelectedIndex = promotion.StartTime.Hour;
                    drpEndTime.SelectedIndex = promotion.EndTime.Hour;
                }
                else
                {
                    chkTime.IsChecked = true;
                    int start = GetIndexForDropdown(promotion.StartTime);
                    int end = GetIndexForDropdown(promotion.EndTime);
                    drpStartTime.Items[start].Selected = true;
                    drpStartTime.SelectedIndex = start;
                    drpEndTime.Items[end].Selected = true;
                    drpEndTime.SelectedIndex = end;
                }
                chkIsTotal.IsChecked = promotion.IsTotal;
                txtDetail.Text = promotion.Detail;
                txtPriceRate.Value = decimal.Parse(promotion.PriceRate.ToString());
                txtFixHour.Value = promotion.FixHour;
                drpPromotionType.Items[promotion.Type].Selected = true;
                drpPromotionType.SelectedIndex = promotion.Type;
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"lstPromotion_SelectedIndexChanged :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private int GetIndexForDropdown(DateTime dateTime)
        {
            try
            {
                string time = dateTime.ToShortTimeString();
                string[] sp = time.Split(new[] { ':' }, StringSplitOptions.None);
                int hour = int.Parse(sp[0]);
                if (time.EndsWith("PM"))
                {
                    hour = hour + 12;
                }
                return hour;
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"GetIndexForDropdown :" + ex.Message);
                return 0;
            }
        }

        private void btnAddPro_Click(object sender, EventArgs e)
        {
            try
            {
                var dlg = new PromotionDialog { StartPosition = FormStartPosition.CenterParent };
                Promotion promotion;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    promotion = dlg.Promotion;
                    var radL = new RadListDataItem(promotion.Name, promotion);
                    lstPromotion.Items.Add(radL);
                    MasterDataHandler.AddPromotion(promotion);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnEditEmp :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEditPro_Click(object sender, EventArgs e)
        {
            try
            {
                var pro = lstPromotion.SelectedItem.Value as Promotion;
                if (pro == null) 
                    return;

                if(txtProName.Text.Length == 0)
                {
                    lblValidate.Text = @"กรูณากรอกข้อมูลให้ครบ";
                    return;
                }

                pro.Name = txtProName.Text;
                pro.StartDate = radStartDate.Value;
                pro.EndDate = radEndDate.Value;

                if (chkTime.IsChecked)
                {
                    pro.StartTime = GetTimeFromDropdown(drpStartTime);
                    pro.EndTime = GetTimeFromDropdown(drpEndTime);
                }
                else
                {
                    pro.StartTime = pro.EndTime = DateTime.Parse("1/1/1753 12:00:00 AM");
                }
                pro.Detail = txtDetail.Text;
                pro.PriceRate = double.Parse(txtPriceRate.Value.ToString());
                pro.Type = drpPromotionType.SelectedIndex;
                pro.FixHour = int.Parse(txtFixHour.Text);
                pro.Detail = txtDetail.Text;
                pro.IsTotal = chkIsTotal.IsChecked;
                lstPromotion.SelectedItem.Value = pro;
                lstPromotion.SelectedItem.Text = pro.Name;
                MasterDataHandler.UpdatePromotion(pro);
                MessageBox.Show(@"บันทึกข้อมูลเรียบร้อย");
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnEditEmp :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeletePro_Click(object sender, EventArgs e)
        {
            try
            {
                var promotion = lstPromotion.SelectedItem.Value as Promotion;
                if (promotion == null) return;

                var confirm = new ConfirmDialog {StartPosition = FormStartPosition.CenterParent};
                if(DialogResult.OK == confirm.ShowDialog())
                {
                    MasterDataHandler.RemovePromotion(promotion.Id);
                    lstPromotion.Items.Remove(lstPromotion.SelectedItem);
                    pnlPromotionData.Enabled = btnDeletePro.Enabled = lstPromotion.Items.Count != 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnDeleteEmp :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        private void ClearForm()
        {
            txtProName.Text = string.Empty;
            radStartDate.Value = DateTime.Now;
            radEndDate.Value = DateTime.Now;
            chkTime.IsChecked = false;
            drpStartTime.SelectedIndex = 0;
            drpEndTime.SelectedIndex = 0;
            txtDetail.Text = string.Empty;
            txtPriceRate.Value = 0;
            txtFixHour.Text = string.Empty;
            drpPromotionType.SelectedIndex = 0;
            chkIsTotal.IsChecked = false;
        }

        private void chkTime_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            grpTime.Enabled = chkTime.IsChecked;
            if (!chkTime.IsChecked)
            {
                drpStartTime.SelectedIndex = drpEndTime.SelectedIndex = 0;
            }
        }

        private void drpStartTime_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            CalculateTime();
        }

        private void drpEndTime_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            CalculateTime();
        }

        private DateTime GetTimeFromDropdown(RadDropDownList drp)
        {
            int time = int.Parse(drp.Text.Substring(0, 2));
            return new DateTime(2000, 1, 1, time, 0, 0);
        }

        private void CalculateTime()
        {
            int start = int.Parse(drpStartTime.Text.Substring(0,2));
            int end = int.Parse(drpEndTime.Text.Substring(0, 2));
            int result;
            if(start > end)
            {
                result = 24 - start + end;
            }
            else
            {
                result = end - start;
            }
            txtFixHour.Value = result;
        }
    }
}
